package com.meilai.project.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meilai.project.entity.system.Config;
import com.meilai.project.mapper.system.ConfigMapper;
import com.meilai.project.service.system.ConfigService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author yxg
 * @Date 2020/8/14 15:38
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private ConfigMapper configMapper;

    @Override
    public List<Config> configActionList() {
        QueryWrapper<Config> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.isNull("deleted_at");
        queryWrapper.orderByAsc("sort");
        return configMapper.selectList(queryWrapper);
    }

    @Override
    public Config configDetail(String code) {
        QueryWrapper<Config> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        queryWrapper.eq("status", 1);
        queryWrapper.isNull("deleted_at");
        return configMapper.selectOne(queryWrapper);
    }

    @Override
    public Config configDetail(String tenantName, String code) {
        QueryWrapper<Config> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        queryWrapper.eq("status", 1);
        queryWrapper.isNull("deleted_at");
        return configMapper.selectOne(queryWrapper);
    }

    @Override
    @Transactional
    public int configEdit(Config createParams) {
        if (createParams.getId() == null) return 0;
        Config obj = configMapper.selectById(createParams.getId());
        if (obj == null) return 0;
        Config _obj = new Config();
        BeanUtils.copyProperties(createParams, _obj);
        return configMapper.updateById(_obj);
    }
}
